package com.swpu.service_remind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_remind.entity.CheckupRemind;
import com.swpu.service_remind.mapper.CheckupRemindMapper;
import com.swpu.service_remind.service.CheckupRemindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-22
 */
@Service
public class CheckupRemindServiceImpl extends ServiceImpl<CheckupRemindMapper, CheckupRemind> implements CheckupRemindService {

    @Override
    public void saveCheckupRemindSetting(String openid, CheckupRemind checkupRemind) {
        LambdaQueryWrapper<CheckupRemind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckupRemind::getUserId,openid);
        CheckupRemind remind = this.getOne(queryWrapper);
        if(remind==null){
            checkupRemind.setUserId(openid);
            boolean save = this.save(checkupRemind);
            if (!save) throw new RuntimeException("保存用户体检通知失败");
        }else {
            remind.setAdded(0);
            remind.setCheckupDate(checkupRemind.getCheckupDate());
            remind.setCheckupPosition(checkupRemind.getCheckupPosition());
            remind.setCheckupNotice(checkupRemind.getCheckupNotice());
            remind.setEnableSms(checkupRemind.getEnableSms());
            remind.setEnableWx(checkupRemind.getEnableWx());
            boolean update = this.updateById(remind);
            if (!update) throw new RuntimeException("更新用户体检通知失败");
        }

    }

    @Override
    public CheckupRemind getCheckupRemindSetting(String openid) {
        LambdaQueryWrapper<CheckupRemind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckupRemind::getUserId,openid);
        return this.getOne(queryWrapper);
    }
}
